﻿#region 命名空间

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Common;

#endregion

namespace WinnerCMS.WebUI.Templates.WinnerCMS6.Member
{
    public partial class GetPassword : WebFactory.ClientPublicPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Wizard1.ActiveStepIndex = 0;
            }
        }

        protected void btnOK_Click1(object sender, EventArgs e)
        {
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            //MemberInfo model = new MemberInfo();
            //model.Name = txtName.Text.Trim();
            //model.Question = HttpContext.Current.Session["Member_Question"].ToString();
            //model.Answer = txtAnswer.Text.Trim();
            //model.PassCode = txtPassCode.Text.Trim();

            //new MemberManage().TryGetPassword2(model);
        }

        protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (e.NextStepIndex == 1)
            {
                if (new MemberManage().CheckUserName(this.txtUserName.Text.Trim()) == false)
                {
                    MsgBox.Message("用户名已经存在。", this.UpdatePanel1);
                    e.Cancel = true;

                    return;
                }

                if (this.RadioButtonList1.SelectedIndex == 0)
                {
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                }
                else if (this.RadioButtonList1.SelectedIndex == 1)
                {
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = true;

                    var str = new MemberManage().LoadQuestion(this.txtUserName.Text.Trim());

                    if (string.IsNullOrEmpty(str))
                    {
                        MsgBox.Message("未设置密码问题", this.UpdatePanel1);
                        e.Cancel = true;
                        return;
                    }
                    this.lblQuestion.Text = str;
                }
            }

            if (e.NextStepIndex == 2)
            {
                if (this.Panel1.Visible)
                {
                    if (new MemberManage().TestEmail(this.txtUserName.Text.Trim(), this.txteMail.Text.Trim()) == false)
                    {
                        MsgBox.Message("电邮地址错误.", this.UpdatePanel1);
                        e.Cancel = true;
                    }
                    else
                    {
                        this.Panel3.Visible = true;
                        this.Panel4.Visible = false;
                    }
                }
                else
                {
                    if (
                        new MemberManage().TestQA(this.txtUserName.Text.Trim(), this.lblQuestion.Text,
                            this.txtAnswer.Text.Trim()) == false)
                    {
                        MsgBox.Message("密码答案错误.", this.UpdatePanel1);
                        e.Cancel = true;
                    }
                    else
                    {
                        this.Panel3.Visible = false;
                        this.Panel4.Visible = true;
                    }
                }
            }
        }

        protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (this.Panel3.Visible)
            {
                var config = CMSConfig.Instance();
                if (config.EMail.IsSend)
                {
                    var NewPSW = Guid.NewGuid().ToString();
                    var mail = new EMail();
                    mail.Send("恭喜您，您在《" + WebConfig.SiteName + "》进行取回密码操纵成功！",
                        "恭喜您，您在《" + WebConfig.SiteName + "》进行取回密码操纵成功<br>您的密码为：" + NewPSW, this.txteMail.Text,
                        config.EMail.From, config.EMail.FromName, config.EMail.Domain, config.EMail.UserName,
                        config.EMail.PassWord);

                    new MemberManage().PasswordModify(this.txtUserName.Text, NewPSW, NewPSW);

                    MsgBox.Message("密码修改完成。", this.UpdatePanel1);

                    ScriptManager.RegisterStartupScript(this.UpdatePanel1, typeof (UpdatePanel), "close",
                        "window.close();", true);
                }
                else
                {
                    MsgBox.Message("管理员关闭邮件发送功能。", this.UpdatePanel1);
                }
            }
            else
            {
                var b = new MemberManage().PasswordModify(this.txtUserName.Text, this.txtPSW1.Text, this.txtPSW2.Text);

                if (b)
                {
                    MsgBox.Message("密码修改成功。", this.UpdatePanel1);
                    ScriptManager.RegisterStartupScript(this.UpdatePanel1, typeof (UpdatePanel), "close",
                        "window.close();", true);
                }
                else
                {
                    MsgBox.Message("两次输入的密码不一致。", this.UpdatePanel1);
                }
            }
        }
    }
}